/*
 *
 * Copyright 2015 gRPC authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

// Package main implements a client for Greeter service.
package main

import (
	"context"
	"encoding/base64"
	"io"
	"log"
	"net/http"
	"os"
	"path/filepath"
	"time"

	pb "ihos/ihos"

	"github.com/gin-contrib/cors"
	"github.com/gin-gonic/gin"
	"github.com/unrolled/secure"
	"google.golang.org/grpc"
	"google.golang.org/protobuf/proto"
)

const (
	// address = "localhost:60051"
	address = "localhost:50051"
	// address     = "106.54.44.82:50051"
	defaultName = "world"
)

func Call_HuanZheRegister(c pb.HospitalClient, ctx context.Context, param []byte) (ret, msg string) {
	ret, msg = "ERROR", "unknown"
	d := &pb.HuanZheInfoL3{}
	if err := proto.Unmarshal(param, d); err != nil {
		msg = "Unmarshal HuanZheInfoL3 Error"
		return
	}

	log.Printf("Call HuanZheRegister, %v", d)
	r, err := c.HuanZheRegister(ctx, d)
	if err != nil {
		msg = "Call HuanZheRegister error:" + err.Error()
		return
	}

	log.Printf("Call HuanZheRegister return, %v", r)

	if m, err := proto.Marshal(r); err != nil {
		msg = "Marshal ReturnMessage Error"
	} else {
		ret, msg = "OK", base64.StdEncoding.EncodeToString(m)
	}
	return
}

func Call_HuanZheHelpRegister(c pb.HospitalClient, ctx context.Context, param []byte) (ret, msg string) {
	ret, msg = "ERROR", "unknown"
	d := &pb.HuanZheInfoL3{}
	if err := proto.Unmarshal(param, d); err != nil {
		msg = "Unmarshal HuanZheInfoL3 Error"
		return
	}

	log.Printf("Call HuanZheHelpRegister, %v", d)
	r, err := c.HuanZheHelpRegister(ctx, d)
	if err != nil {
		msg = "Call HuanZheHelpRegister error:" + err.Error()
		return
	}

	log.Printf("Call HuanZheHelpRegister return, %v", r)

	if m, err := proto.Marshal(r); err != nil {
		msg = "Marshal ReturnMessage Error"
	} else {
		ret, msg = "OK", base64.StdEncoding.EncodeToString(m)
	}
	return
}

func Call_Login(c pb.HospitalClient, ctx context.Context, param []byte) (ret, msg string) {
	ret, msg = "ERROR", "unknown"
	d := &pb.DengLuL2{}
	if err := proto.Unmarshal(param, d); err != nil {
		msg = "Unmarshal DengLuL2 Error"
		return
	}

	log.Printf("Call Login, %v", d)
	r, err := c.Login(ctx, d)
	if err != nil {
		msg = "Call Login error:" + err.Error()
		return
	}

	log.Printf("Call Login return, %v", r)

	if m, err := proto.Marshal(r); err != nil {
		msg = "Marshal DengLuShuJuL1 Error"
	} else {
		ret, msg = "OK", base64.StdEncoding.EncodeToString(m)
	}
	return
}

func Call_GetHuanZheInfo(c pb.HospitalClient, ctx context.Context, param []byte) (ret, msg string) {
	ret, msg = "ERROR", "unknown"
	d := &pb.HuanZheInfoL3{}
	if err := proto.Unmarshal(param, d); err != nil {
		msg = "Unmarshal HuanZheInfoL3 Error"
		return
	}

	log.Printf("Call GetHuanZheInfo, %v", d)
	r, err := c.GetHuanZheInfo(ctx, d)
	if err != nil {
		msg = "Call GetHuanZheInfo error:" + err.Error()
		return
	}

	log.Printf("Call GetHuanZheInfo return, %v", r)

	if m, err := proto.Marshal(r); err != nil {
		msg = "Marshal ReturnMessage Error"
	} else {
		ret, msg = "OK", base64.StdEncoding.EncodeToString(m)
	}
	return
}

func Call_GetYiShengInfo(c pb.HospitalClient, ctx context.Context, param []byte) (ret, msg string) {
	ret, msg = "ERROR", "unknown"
	d := &pb.YiShengInfoL3{}
	if err := proto.Unmarshal(param, d); err != nil {
		msg = "Unmarshal YiShengInfoL3 Error"
		return
	}

	log.Printf("Call GetYiShengInfo, %v", d)
	r, err := c.GetYiShengInfo(ctx, d)
	if err != nil {
		msg = "Call GetYiShengInfo error:" + err.Error()
		return
	}

	log.Printf("Call GetYiShengInfo return, %v", r)

	if m, err := proto.Marshal(r); err != nil {
		msg = "Marshal YiShengListL4 Error"
	} else {
		ret, msg = "OK", base64.StdEncoding.EncodeToString(m)
	}
	return
}

func Call_GetKeFuInfo(c pb.HospitalClient, ctx context.Context, param []byte) (ret, msg string) {
	ret, msg = "ERROR", "unknown"
	d := &pb.KeFuInfoL3{}
	if err := proto.Unmarshal(param, d); err != nil {
		msg = "Unmarshal KeFuInfoL3 Error"
		return
	}

	log.Printf("Call GetKeFuInfo, %v", d)
	r, err := c.GetKeFuInfo(ctx, d)
	if err != nil {
		msg = "Call GetKeFuInfo error:" + err.Error()
		return
	}

	log.Printf("Call GetKeFuInfo return, %v", r)

	if m, err := proto.Marshal(r); err != nil {
		msg = "Marshal KeFuInfoL3 Error"
	} else {
		ret, msg = "OK", base64.StdEncoding.EncodeToString(m)
	}
	return
}

func Call_SaveHuanZheInfo(c pb.HospitalClient, ctx context.Context, param []byte) (ret, msg string) {
	ret, msg = "ERROR", "unknown"
	d := &pb.HuanZheInfoL3{}
	if err := proto.Unmarshal(param, d); err != nil {
		msg = "Unmarshal HuanZheInfoL3 Error"
		return
	}

	log.Printf("Call SaveHuanZheInfo, %v", d)
	log.Println("Param:", param)
	r, err := c.SaveHuanZheInfo(ctx, d)
	if err != nil {
		msg = "Call SaveHuanZheInfo error:" + err.Error()
		return
	}

	log.Printf("Call SaveHuanZheInfo return, %v", r)

	if m, err := proto.Marshal(r); err != nil {
		msg = "Marshal ReturnMessage Error"
	} else {
		ret, msg = "OK", base64.StdEncoding.EncodeToString(m)
	}
	return
}

func Call_GetWXSession(c pb.HospitalClient, ctx context.Context, param []byte) (ret, msg string) {
	ret, msg = "ERROR", "unknown"
	d := &pb.WXCode{}
	if err := proto.Unmarshal(param, d); err != nil {
		msg = "Unmarshal WXCode Error"
		return
	}

	log.Printf("Call GetWXSession-1, %v", d)
	r, err := c.GetWXSession(ctx, d)
	if err != nil {
		msg = "Call GetWXSession error:" + err.Error()
		return
	}

	log.Printf("Call GetWXSession return, %v", r)

	if m, err := proto.Marshal(r); err != nil {
		msg = "Marshal WXSession Error"
	} else {
		ret, msg = "OK", base64.StdEncoding.EncodeToString(m)
	}
	return
}

func Call_DecodeWXUserInfo(c pb.HospitalClient, ctx context.Context, param []byte) (ret, msg string) {
	ret, msg = "ERROR", "unknown"
	d := &pb.WXEncData{}
	if err := proto.Unmarshal(param, d); err != nil {
		msg = "Unmarshal WXEncData Error"
		return
	}

	log.Printf("Call DecodeWXUserInfo-1, %v", d)
	r, err := c.DecodeWXUserInfo(ctx, d)
	if err != nil {
		msg = "Call SaveHuanZheInfo error:" + err.Error()
		return
	}

	log.Printf("Call DecodeWXUserInfo return, %v", r)

	if m, err := proto.Marshal(r); err != nil {
		msg = "Marshal ReturnMessage Error"
	} else {
		ret, msg = "OK", base64.StdEncoding.EncodeToString(m)
	}
	return
}

func Call_CallZYAPI(c pb.HospitalClient, ctx context.Context, param []byte) (ret, msg string) {
	ret, msg = "ERROR", "unknown"
	d := &pb.ZYAPIParam{}
	if err := proto.Unmarshal(param, d); err != nil {
		msg = "Unmarshal ZYAPIParam Error"
		return
	}

	log.Printf("Call CallZYAPI, %v", d)
	log.Printf("body:", d.MSGBODY)
	r, err := c.CallZYAPI(ctx, d)
	if err != nil {
		msg = "Call CallZYAPI error:" + err.Error()
		return
	}

	// log.Printf("Call CallZYAPI return, %v", r)

	if m, err := proto.Marshal(r); err != nil {
		msg = "Marshal ReturnMessage Error"
	} else {
		ret, msg = "OK", base64.StdEncoding.EncodeToString(m)
	}
	return
}

func Call_MakeVideoCall(c pb.HospitalClient, ctx context.Context, param []byte) (ret, msg string) {
	ret, msg = "ERROR", "unknown"
	d := &pb.ShiPinYongHuL5{}
	if err := proto.Unmarshal(param, d); err != nil {
		msg = "Unmarshal ShiPinYongHuL5 Error"
		return
	}
	log.Printf("Call MakeVideoCall, %v", d)
	r, err := c.MakeVideoCall(ctx, d)
	if err != nil {
		msg = "Call MakeVideoCall error:" + err.Error()
		return
	}

	log.Printf("Call MakeVideoCall return, %v", r)

	if m, err := proto.Marshal(r); err != nil {
		msg = "Marshal ShiPinYongHuL5 Error"
	} else {
		ret, msg = "OK", base64.StdEncoding.EncodeToString(m)
	}
	return
}

func Call_AcceptVideoCall(c pb.HospitalClient, ctx context.Context, param []byte) (ret, msg string) {
	ret, msg = "ERROR", "unknown"
	d := &pb.ShiPinYongHuL5{}
	if err := proto.Unmarshal(param, d); err != nil {
		msg = "Unmarshal ShiPinYongHuL5 Error"
		return
	}
	log.Printf("Call AcceptVideoCall, %v", d)
	r, err := c.AcceptVideoCall(ctx, d)
	if err != nil {
		msg = "Call AcceptVideoCall error:" + err.Error()
		return
	}

	log.Printf("Call AcceptVideoCall return, %v", r)

	if m, err := proto.Marshal(r); err != nil {
		msg = "Marshal ShiPinYongHuL5 Error"
	} else {
		ret, msg = "OK", base64.StdEncoding.EncodeToString(m)
	}
	return
}

func Call_HangUpVideoCall(c pb.HospitalClient, ctx context.Context, param []byte) (ret, msg string) {
	ret, msg = "ERROR", "unknown"
	d := &pb.ShiPinYongHuL5{}
	if err := proto.Unmarshal(param, d); err != nil {
		msg = "Unmarshal ShiPinYongHuL5 Error"
		return
	}
	log.Printf("Call HangUpVideoCall, %v", d)
	r, err := c.HangUpVideoCall(ctx, d)
	if err != nil {
		msg = "Call HangUpVideoCall error:" + err.Error()
		return
	}

	log.Printf("Call HangUpVideoCall return, %v", r)

	if m, err := proto.Marshal(r); err != nil {
		msg = "Marshal ShiPinYongHuL5 Error"
	} else {
		ret, msg = "OK", base64.StdEncoding.EncodeToString(m)
	}
	return
}

// 客服工作站，对视频来电患者挂号后可以直接转去医生房间，需要预先设置一条视频信息:
// insert into shipin(id,fjh,cjz,cjsj,gbsj,zt,lxdz,lx,yhid,yhlx) values(123,'123456','cjz233',1572245478,1717631593,2,'111.22.22.22',2,0,0);
func Call_RequestJoinVideoCall(c pb.HospitalClient, ctx context.Context, param []byte) (ret, msg string) {
	ret, msg = "ERROR", "unknown"
	d := &pb.ShiPinRequestL6{}
	if err := proto.Unmarshal(param, d); err != nil {
		msg = "Unmarshal ShiPinRequestL6 Error"
		return
	}
	log.Printf("Call RequestJoinVideoCall, %v", d)
	r, err := c.RequestJoinVideoCall(ctx, d)
	if err != nil {
		msg = "Call RequestJoinVideoCall error:" + err.Error()
		return
	}

	log.Printf("Call RequestJoinVideoCall return, %v", r)

	if m, err := proto.Marshal(r); err != nil {
		msg = "Marshal ShiPinRequestL6 Error"
	} else {
		ret, msg = "OK", base64.StdEncoding.EncodeToString(m)
	}
	return
}

func Call_CancelVideoCallRequest(c pb.HospitalClient, ctx context.Context, param []byte) (ret, msg string) {
	ret, msg = "ERROR", "unknown"
	d := &pb.ShiPinRequestL6{}
	if err := proto.Unmarshal(param, d); err != nil {
		msg = "Unmarshal ShiPinRequestL6 Error"
		return
	}
	log.Printf("Call RequestJoinVideoCall, %v", d)
	r, err := c.CancelVideoCallRequest(ctx, d)
	if err != nil {
		msg = "Call CancelVideoCallRequest error:" + err.Error()
		return
	}

	log.Printf("Call CancelVideoCallRequest return, %v", r)

	if m, err := proto.Marshal(r); err != nil {
		msg = "Marshal ShiPinRequestL6 Error"
	} else {
		ret, msg = "OK", base64.StdEncoding.EncodeToString(m)
	}
	return
}

func Call_RequestHangUpVideoCall(c pb.HospitalClient, ctx context.Context, param []byte) (ret, msg string) {
	ret, msg = "ERROR", "unknown"
	d := &pb.ShiPinRequestL6{}
	if err := proto.Unmarshal(param, d); err != nil {
		msg = "Unmarshal ShiPinRequestL6 Error"
		return
	}
	log.Printf("Call RequestHangUpVideoCall, %v", d)
	r, err := c.RequestHangUpVideoCall(ctx, d)
	if err != nil {
		msg = "Call RequestHangUpVideoCall error:" + err.Error()
		return
	}

	log.Printf("Call RequestHangUpVideoCall return, %v", r)

	if m, err := proto.Marshal(r); err != nil {
		msg = "Marshal ShiPinRequestL6 Error"
	} else {
		ret, msg = "OK", base64.StdEncoding.EncodeToString(m)
	}
	return
}

func Call_GetVideoCallRequestList(c pb.HospitalClient, ctx context.Context, param []byte) (ret, msg string) {
	ret, msg = "ERROR", "unknown"
	d := &pb.ShiPinYongHuL5{}
	if err := proto.Unmarshal(param, d); err != nil {
		msg = "Unmarshal ShiPinYongHuL5 Error"
		return
	}
	log.Printf("Call GetVideoCallRequestList, %v", d)
	r, err := c.GetVideoCallRequestList(ctx, d)
	if err != nil {
		msg = "Call GetVideoCallRequestList error:" + err.Error()
		return
	}

	// log.Printf("Call GetVideoCallRequestList return, %v", r)
	log.Printf("Call GetVideoCallRequestList return")

	if m, err := proto.Marshal(r); err != nil {
		msg = "Marshal ShiPinRequestListL7 Error"
	} else {
		// log.Println(m)
		ret, msg = "OK", base64.StdEncoding.EncodeToString(m)
	}
	return
}

func Call_TakeVideoCallRequest(c pb.HospitalClient, ctx context.Context, param []byte) (ret, msg string) {
	ret, msg = "ERROR", "unknown"
	d := &pb.ShiPinYongHuL5{}
	if err := proto.Unmarshal(param, d); err != nil {
		msg = "Unmarshal ShiPinYongHuL5 Error"
		return
	}
	log.Printf("Call TakeVideoCallRequest, %v", d)
	r, err := c.TakeVideoCallRequest(ctx, d)
	if err != nil {
		msg = "Call TakeVideoCallRequest error:" + err.Error()
		return
	}

	log.Printf("Call TakeVideoCallRequest return, %v", r)

	if m, err := proto.Marshal(r); err != nil {
		msg = "Marshal ShiPinRequestL6 Error"
	} else {
		ret, msg = "OK", base64.StdEncoding.EncodeToString(m)
	}
	return
}

func Call_SaveShiPinRequestL6(c pb.HospitalClient, ctx context.Context, param []byte) (ret, msg string) {
	ret, msg = "ERROR", "unknown"
	d := &pb.ShiPinRequestL6{}
	if err := proto.Unmarshal(param, d); err != nil {
		msg = "Unmarshal ShiPinRequestL6 Error"
		return
	}
	log.Printf("Call SaveShiPinRequestL6, %v", d)
	r, err := c.SaveShiPinRequestL6(ctx, d)
	if err != nil {
		msg = "Call SaveShiPinRequestL6 error:" + err.Error()
		return
	}

	log.Printf("Call SaveShiPinRequestL6 return, %v", r)

	if m, err := proto.Marshal(r); err != nil {
		msg = "Marshal ShiPinRequestL6 Error"
	} else {
		ret, msg = "OK", base64.StdEncoding.EncodeToString(m)
	}
	return
}

func Call_RemoveVideoCallRequest(c pb.HospitalClient, ctx context.Context, param []byte) (ret, msg string) {
	ret, msg = "ERROR", "unknown"
	d := &pb.ShiPinRequestL6{}
	if err := proto.Unmarshal(param, d); err != nil {
		msg = "Unmarshal ShiPinRequestL6 Error"
		return
	}
	log.Printf("Call RemoveVideoCallRequest, %v", d)
	r, err := c.RemoveVideoCallRequest(ctx, d)
	if err != nil {
		msg = "Call RemoveVideoCallRequest error:" + err.Error()
		return
	}

	log.Printf("Call RemoveVideoCallRequest return, %v", r)

	if m, err := proto.Marshal(r); err != nil {
		msg = "Marshal ShiPinRequestL6 Error"
	} else {
		ret, msg = "OK", base64.StdEncoding.EncodeToString(m)
	}
	return
}

func Call_GetGuaHaoList(c pb.HospitalClient, ctx context.Context, param []byte) (ret, msg string) {
	ret, msg = "ERROR", "unknown"
	d := &pb.GuaHao{}
	if err := proto.Unmarshal(param, d); err != nil {
		msg = "Unmarshal GuaHao Error"
		return
	}
	log.Printf("Call GetGuaHaoList, %v", d)
	r, err := c.GetGuaHaoList(ctx, d)
	if err != nil {
		msg = "Call GetGuaHaoList error:" + err.Error()
		return
	}

	// log.Printf("Call GetGuaHaoList return, %v", r)

	if m, err := proto.Marshal(r); err != nil {
		msg = "Marshal GuaHaoList Error"
	} else {
		ret, msg = "OK", base64.StdEncoding.EncodeToString(m)
	}
	return
}

func Call_GetGuaHaoDuiZhangList(c pb.HospitalClient, ctx context.Context, param []byte) (ret, msg string) {
	ret, msg = "ERROR", "unknown"
	d := &pb.GuaHao{}
	if err := proto.Unmarshal(param, d); err != nil {
		msg = "Unmarshal GuaHao Error"
		return
	}
	log.Printf("Call GetGuaHaoDuiZhangList, %v", d)
	r, err := c.GetGuaHaoDuiZhangList(ctx, d)
	if err != nil {
		msg = "Call GetGuaHaoDuiZhangList error:" + err.Error()
		return
	}

	// log.Printf("Call GetGuaHaoDuiZhangList return, %v", r)

	if m, err := proto.Marshal(r); err != nil {
		msg = "Marshal GuaHaoList Error"
	} else {
		ret, msg = "OK", base64.StdEncoding.EncodeToString(m)
	}
	return
}

func Call_SaveGuaHao(c pb.HospitalClient, ctx context.Context, param []byte) (ret, msg string) {
	ret, msg = "ERROR", "unknown"
	d := &pb.GuaHao{}
	if err := proto.Unmarshal(param, d); err != nil {
		msg = "Unmarshal GuaHao Error"
		return
	}
	log.Printf("Call SaveGuaHao, %v", d)
	r, err := c.SaveGuaHao(ctx, d)
	if err != nil {
		msg = "Call SaveGuaHao error:" + err.Error()
		return
	}

	log.Printf("Call SaveGuaHao return, %v", r)

	if m, err := proto.Marshal(r); err != nil {
		msg = "Marshal ReturnMessage Error"
	} else {
		ret, msg = "OK", base64.StdEncoding.EncodeToString(m)
	}
	return
}

func Call_GetShiPinList(c pb.HospitalClient, ctx context.Context, param []byte) (ret, msg string) {
	ret, msg = "ERROR", "unknown"
	d := &pb.ShiPinInfoL1{}
	if err := proto.Unmarshal(param, d); err != nil {
		msg = "Unmarshal ShiPinInfoL1 Error"
		return
	}
	log.Printf("Call GetShiPinList, %v", d)
	r, err := c.GetShiPinList(ctx, d)
	if err != nil {
		msg = "Call GetShiPinList error:" + err.Error()
		return
	}

	log.Printf("Call GetShiPinList return, %v", r)

	if m, err := proto.Marshal(r); err != nil {
		msg = "Marshal GetShiPinList Error"
	} else {
		ret, msg = "OK", base64.StdEncoding.EncodeToString(m)
	}
	return
}

func Call_GetMenZhenPaiBanList(c pb.HospitalClient, ctx context.Context, param []byte) (ret, msg string) {
	ret, msg = "ERROR", "unknown"
	d := &pb.MenZhenPaiBan{}
	if err := proto.Unmarshal(param, d); err != nil {
		msg = "Unmarshal MenZhenPaiBan Error"
		return
	}
	log.Printf("Call GetMenZhenPaiBanList, %v", d)
	r, err := c.GetMenZhenPaiBanList(ctx, d)
	if err != nil {
		msg = "Call GetMenZhenPaiBanList error:" + err.Error()
		return
	}

	// log.Printf("Call GetMenZhenPaiBanList return, %v", r)
	log.Printf("Call GetMenZhenPaiBanList return")

	if m, err := proto.Marshal(r); err != nil {
		msg = "Marshal GetShiPinList Error"
	} else {
		ret, msg = "OK", base64.StdEncoding.EncodeToString(m)
	}
	return
}

func Call_MenZhenCurrent(c pb.HospitalClient, ctx context.Context, param []byte) (ret, msg string) {
	ret, msg = "ERROR", "unknown"
	d := &pb.ShiPinYongHuL5{}
	if err := proto.Unmarshal(param, d); err != nil {
		msg = "Unmarshal ShiPinYongHuL5 Error"
		return
	}
	log.Printf("Call MenZhenCurrent, %v", d)
	r, err := c.MenZhenCurrent(ctx, d)
	if err != nil {
		msg = "Call MenZhenCurrent error:" + err.Error()
		return
	}

	log.Printf("Call MenZhenCurrent return, %v", r)

	if m, err := proto.Marshal(r); err != nil {
		msg = "Marshal ReturnMessage Error"
	} else {
		ret, msg = "OK", base64.StdEncoding.EncodeToString(m)
	}
	return
}

func Call_MenZhenJiaoHao(c pb.HospitalClient, ctx context.Context, param []byte) (ret, msg string) {
	ret, msg = "ERROR", "unknown"
	d := &pb.ShiPinYongHuL5{}
	if err := proto.Unmarshal(param, d); err != nil {
		msg = "Unmarshal ShiPinYongHuL5 Error"
		return
	}
	log.Printf("Call MenZhenJiaoHao, %v", d)
	r, err := c.MenZhenJiaoHao(ctx, d)
	if err != nil {
		msg = "Call MenZhenJiaoHao error:" + err.Error()
		return
	}

	log.Printf("Call MenZhenJiaoHao return, %v", r)

	if m, err := proto.Marshal(r); err != nil {
		msg = "Marshal ReturnMessage Error"
	} else {
		ret, msg = "OK", base64.StdEncoding.EncodeToString(m)
	}
	return
}

func Call_MenZhenNext(c pb.HospitalClient, ctx context.Context, param []byte) (ret, msg string) {
	ret, msg = "ERROR", "unknown"
	d := &pb.ShiPinYongHuL5{}
	if err := proto.Unmarshal(param, d); err != nil {
		msg = "Unmarshal ShiPinYongHuL5 Error"
		return
	}
	log.Printf("Call MenZhenNext, %v", d)
	r, err := c.MenZhenNext(ctx, d)
	if err != nil {
		msg = "Call MenZhenNext error:" + err.Error()
		return
	}

	log.Printf("Call MenZhenNext return, %v", r)

	if m, err := proto.Marshal(r); err != nil {
		msg = "Marshal ReturnMessage Error"
	} else {
		ret, msg = "OK", base64.StdEncoding.EncodeToString(m)
	}
	return
}

func Call_MenZhenGuoHao(c pb.HospitalClient, ctx context.Context, param []byte) (ret, msg string) {
	ret, msg = "ERROR", "unknown"
	d := &pb.ShiPinYongHuL5{}
	if err := proto.Unmarshal(param, d); err != nil {
		msg = "Unmarshal ShiPinYongHuL5 Error"
		return
	}
	log.Printf("Call MenZhenGuoHao, %v", d)
	r, err := c.MenZhenGuoHao(ctx, d)
	if err != nil {
		msg = "Call MenZhenGuoHao error:" + err.Error()
		return
	}

	log.Printf("Call MenZhenGuoHao return, %v", r)

	if m, err := proto.Marshal(r); err != nil {
		msg = "Marshal ReturnMessage Error"
	} else {
		ret, msg = "OK", base64.StdEncoding.EncodeToString(m)
	}
	return
}

func Call_MenZhenQuery(c pb.HospitalClient, ctx context.Context, param []byte) (ret, msg string) {
	ret, msg = "ERROR", "unknown"
	d := &pb.ShiPinYongHuL5{}
	if err := proto.Unmarshal(param, d); err != nil {
		msg = "Unmarshal ShiPinYongHuL5 Error"
		return
	}
	log.Printf("Call MenZhenQuery, %v", d)
	r, err := c.MenZhenQuery(ctx, d)
	if err != nil {
		msg = "Call MenZhenQuery error:" + err.Error()
		return
	}

	log.Printf("Call MenZhenQuery return, %v", r)

	if m, err := proto.Marshal(r); err != nil {
		msg = "Marshal ReturnMessage Error"
	} else {
		ret, msg = "OK", base64.StdEncoding.EncodeToString(m)
	}
	return
}

func Call_MenZhenWanCheng(c pb.HospitalClient, ctx context.Context, param []byte) (ret, msg string) {
	ret, msg = "ERROR", "unknown"
	d := &pb.ShiPinYongHuL5{}
	if err := proto.Unmarshal(param, d); err != nil {
		msg = "Unmarshal ShiPinYongHuL5 Error"
		return
	}
	log.Printf("Call MenZhenWanCheng, %v", d)
	r, err := c.MenZhenWanCheng(ctx, d)
	if err != nil {
		msg = "Call MenZhenWanCheng error:" + err.Error()
		return
	}

	log.Printf("Call MenZhenWanCheng return, %v", r)

	if m, err := proto.Marshal(r); err != nil {
		msg = "Marshal ReturnMessage Error"
	} else {
		ret, msg = "OK", base64.StdEncoding.EncodeToString(m)
	}
	return
}

func Call_MenZhenInform(c pb.HospitalClient, ctx context.Context, param []byte) (ret, msg string) {
	ret, msg = "ERROR", "unknown"
	d := &pb.ShiPinYongHuL5{}
	if err := proto.Unmarshal(param, d); err != nil {
		msg = "Unmarshal ShiPinYongHuL5 Error"
		return
	}
	log.Printf("Call MenZhenInform, %v", d)
	r, err := c.MenZhenInform(ctx, d)
	if err != nil {
		msg = "Call MenZhenInform error:" + err.Error()
		return
	}

	log.Printf("Call MenZhenInform return, %v", r)

	if m, err := proto.Marshal(r); err != nil {
		msg = "Marshal ReturnMessage Error"
	} else {
		ret, msg = "OK", base64.StdEncoding.EncodeToString(m)
	}
	return
}

func Call_DengLuShuJuVerify(c pb.HospitalClient, ctx context.Context, param []byte) (ret, msg string) {
	ret, msg = "ERROR", "unknown"
	d := &pb.DengLuShuJuL1{}
	if err := proto.Unmarshal(param, d); err != nil {
		msg = "Unmarshal DengLuShuJuL1 Error"
		return
	}
	log.Printf("Call DengLuShuJuVerify, %v", d)
	r, err := c.DengLuShuJuVerify(ctx, d)
	if err != nil {
		msg = "Call DengLuShuJuVerify error:" + err.Error()
		return
	}

	log.Printf("Call DengLuShuJuVerify return, %v", r)

	if m, err := proto.Marshal(r); err != nil {
		msg = "Marshal ReturnMessage Error"
	} else {
		ret, msg = "OK", base64.StdEncoding.EncodeToString(m)
	}
	return
}

func Call_GenerateUserSig(c pb.HospitalClient, ctx context.Context, param []byte) (ret, msg string) {
	ret, msg = "ERROR", "unknown"
	d := &pb.UserID{}
	if err := proto.Unmarshal(param, d); err != nil {
		msg = "Unmarshal UserID Error"
		return
	}
	log.Printf("Call GenerateUserSig, %v", d)
	r, err := c.GenerateUserSig(ctx, d)
	if err != nil {
		msg = "Call GenerateUserSig error:" + err.Error()
		return
	}

	log.Printf("Call GenerateUserSig return, %v", r)

	if m, err := proto.Marshal(r); err != nil {
		msg = "Marshal ReturnMessage Error"
	} else {
		ret, msg = "OK", base64.StdEncoding.EncodeToString(m)
	}
	return
}

func Call_GetHouZhenPaiDuiList(c pb.HospitalClient, ctx context.Context, param []byte) (ret, msg string) {
	ret, msg = "ERROR", "unknown"
	d := &pb.HouZhenPaiDui{}
	if err := proto.Unmarshal(param, d); err != nil {
		msg = "Unmarshal HouZhenPaiDui Error"
		return
	}
	log.Printf("Call GetHouZhenPaiDuiList, %v", d)
	r, err := c.GetHouZhenPaiDuiList(ctx, d)
	if err != nil {
		msg = "Call GetHouZhenPaiDuiList error:" + err.Error()
		return
	}

	// log.Printf("Call GetHouZhenPaiDuiList return, %v", r)

	if m, err := proto.Marshal(r); err != nil {
		msg = "Marshal ReturnMessage Error"
	} else {
		ret, msg = "OK", base64.StdEncoding.EncodeToString(m)
	}
	return
}

func Call_GetVideoCallNeedResume(c pb.HospitalClient, ctx context.Context, param []byte) (ret, msg string) {
	ret, msg = "ERROR", "unknown"
	d := &pb.ShiPinYongHuL5{}
	if err := proto.Unmarshal(param, d); err != nil {
		msg = "Unmarshal ShiPinYongHuL5 Error"
		return
	}
	log.Printf("Call GetVideoCallNeedResume, %v", d)
	r, err := c.GetVideoCallNeedResume(ctx, d)
	if err != nil {
		msg = "Call GetVideoCallNeedResume error:" + err.Error()
		return
	}

	log.Printf("Call GetVideoCallNeedResume return, %v", r)

	if m, err := proto.Marshal(r); err != nil {
		msg = "Marshal ReturnMessage Error"
	} else {
		ret, msg = "OK", base64.StdEncoding.EncodeToString(m)
	}
	return
}

func Call_PaiDuiJiaoHao(c pb.HospitalClient, ctx context.Context, param []byte) (ret, msg string) {
	ret, msg = "ERROR", "unknown"
	d := &pb.HouZhenPaiDui{}
	if err := proto.Unmarshal(param, d); err != nil {
		msg = "Unmarshal HouZhenPaiDui Error"
		return
	}
	log.Printf("Call PaiDuiJiaoHao, %v", d)
	r, err := c.PaiDuiJiaoHao(ctx, d)
	if err != nil {
		msg = "Call PaiDuiJiaoHao error:" + err.Error()
		return
	}

	log.Printf("Call PaiDuiJiaoHao return, %v", r)

	if m, err := proto.Marshal(r); err != nil {
		msg = "Marshal HouZhenPaiDui Error"
	} else {
		ret, msg = "OK", base64.StdEncoding.EncodeToString(m)
	}
	return
}

func Call_PaiDuiSetStatus(c pb.HospitalClient, ctx context.Context, param []byte) (ret, msg string) {
	ret, msg = "ERROR", "unknown"
	d := &pb.HouZhenPaiDui{}
	if err := proto.Unmarshal(param, d); err != nil {
		msg = "Unmarshal HouZhenPaiDui Error"
		return
	}
	log.Printf("Call PaiDuiSetStatus, %v", d)
	r, err := c.PaiDuiSetStatus(ctx, d)
	if err != nil {
		msg = "Call PaiDuiSetStatus error:" + err.Error()
		return
	}

	log.Printf("Call PaiDuiSetStatus return, %v", r)

	if m, err := proto.Marshal(r); err != nil {
		msg = "Marshal HouZhenPaiDui Error"
	} else {
		ret, msg = "OK", base64.StdEncoding.EncodeToString(m)
	}
	return
}

func Call_SaveGuaHaoShuJu(c pb.HospitalClient, ctx context.Context, param []byte) (ret, msg string) {
	ret, msg = "ERROR", "unknown"
	d := &pb.GuaHaoShuJu{}
	if err := proto.Unmarshal(param, d); err != nil {
		msg = "Unmarshal GuaHaoShuJu Error"
		return
	}
	log.Printf("Call SaveGuaHaoShuJu, %v", d.ID)
	r, err := c.SaveGuaHaoShuJu(ctx, d)
	if err != nil {
		msg = "Call SaveGuaHaoShuJu error:" + err.Error()
		log.Println("err:", msg)
		return
	}

	log.Printf("Call SaveGuaHaoShuJu return, %v", r)

	if m, err := proto.Marshal(r); err != nil {
		msg = "Marshal GuaHaoShuJu Error"
	} else {
		ret, msg = "OK", base64.StdEncoding.EncodeToString(m)
	}
	return
}

func Call_GetGuaHaoShuJu(c pb.HospitalClient, ctx context.Context, param []byte) (ret, msg string) {
	ret, msg = "ERROR", "unknown"
	d := &pb.GuaHaoShuJu{}
	if err := proto.Unmarshal(param, d); err != nil {
		msg = "Unmarshal GuaHaoShuJu Error"
		return
	}
	log.Printf("Call GetGuaHaoShuJu, %v", d)
	r, err := c.GetGuaHaoShuJu(ctx, d)
	if err != nil {
		msg = "Call GetGuaHaoShuJu error:" + err.Error()
		return
	}

	log.Printf("Call GetGuaHaoShuJu return, %v", r)

	if m, err := proto.Marshal(r); err != nil {
		msg = "Marshal GuaHaoShuJu Error"
	} else {
		ret, msg = "OK", base64.StdEncoding.EncodeToString(m)
	}
	return
}

func Call_GetHospital(c pb.HospitalClient, ctx context.Context, param []byte) (ret, msg string) {
	ret, msg = "ERROR", "unknown"
	d := &pb.YiYuan{}
	if err := proto.Unmarshal(param, d); err != nil {
		msg = "Unmarshal YiYuan Error"
		return
	}
	log.Printf("Call GetHospital, %v", d)
	r, err := c.GetHospital(ctx, d)
	if err != nil {
		msg = "Call GetHospital error:" + err.Error()
		return
	}

	log.Printf("Call GetHospital return, %v", r)

	if m, err := proto.Marshal(r); err != nil {
		msg = "Marshal YiYuan Error"
	} else {
		ret, msg = "OK", base64.StdEncoding.EncodeToString(m)
	}
	return
}

func Call_GetHospitalWithinDistance(c pb.HospitalClient, ctx context.Context, param []byte) (ret, msg string) {
	ret, msg = "ERROR", "unknown"
	d := &pb.AreaPointRange{}
	if err := proto.Unmarshal(param, d); err != nil {
		msg = "Unmarshal AreaPointRange Error"
		return
	}
	log.Printf("Call GetHospitalWithinDistance, %v", d)
	r, err := c.GetHospitalWithinDistance(ctx, d)
	if err != nil {
		msg = "Call GetHospitalWithinDistance error:" + err.Error()
		return
	}

	log.Printf("Call GetHospitalWithinDistance return, %v", r)

	if m, err := proto.Marshal(r); err != nil {
		msg = "Marshal YiYuanList Error"
	} else {
		ret, msg = "OK", base64.StdEncoding.EncodeToString(m)
	}
	return
}

func Call_GetHospitalByArea(c pb.HospitalClient, ctx context.Context, param []byte) (ret, msg string) {
	ret, msg = "ERROR", "unknown"
	d := &pb.Area{}
	if err := proto.Unmarshal(param, d); err != nil {
		msg = "Unmarshal Area Error"
		return
	}
	log.Printf("Call GetHospitalByArea, %v", d)
	r, err := c.GetHospitalByArea(ctx, d)
	if err != nil {
		msg = "Call GetHospitalByArea error:" + err.Error()
		return
	}

	log.Printf("Call GetHospitalByArea return, %v", r)

	if m, err := proto.Marshal(r); err != nil {
		msg = "Marshal YiYuanList Error"
	} else {
		ret, msg = "OK", base64.StdEncoding.EncodeToString(m)
	}
	return
}

func Call_GetUserStat(c pb.HospitalClient, ctx context.Context, param []byte) (ret, msg string) {
	ret, msg = "ERROR", "unknown"
	d := &pb.UserStat{}
	if err := proto.Unmarshal(param, d); err != nil {
		msg = "Unmarshal UserStat Error"
		return
	}
	log.Printf("Call GetUserStat, %v", d)
	r, err := c.GetUserStat(ctx, d)
	if err != nil {
		msg = "Call GetUserStat error:" + err.Error()
		return
	}

	log.Printf("Call GetUserStat return, %v", r)

	if m, err := proto.Marshal(r); err != nil {
		msg = "Marshal UserStatList Error"
	} else {
		ret, msg = "OK", base64.StdEncoding.EncodeToString(m)
	}
	return
}

func Call_GetVip(c pb.HospitalClient, ctx context.Context, param []byte) (ret, msg string) {
	ret, msg = "ERROR", "unknown"
	d := &pb.Vip{}
	if err := proto.Unmarshal(param, d); err != nil {
		msg = "Unmarshal Vip Error"
		return
	}
	log.Printf("Call GetVip, %v", d)
	r, err := c.GetVip(ctx, d)
	if err != nil {
		msg = "Call GetVip error:" + err.Error()
		return
	}

	log.Printf("Call GetVip return, %v", r)

	if m, err := proto.Marshal(r); err != nil {
		msg = "Marshal UserStatList Error"
	} else {
		ret, msg = "OK", base64.StdEncoding.EncodeToString(m)
	}
	return
}

func Call_SetVip(c pb.HospitalClient, ctx context.Context, param []byte) (ret, msg string) {
	ret, msg = "ERROR", "unknown"
	d := &pb.Vip{}
	if err := proto.Unmarshal(param, d); err != nil {
		msg = "Unmarshal Vip Error"
		return
	}
	log.Printf("Call SetVip, %v", d)
	r, err := c.SetVip(ctx, d)
	if err != nil {
		msg = "Call SetVip error:" + err.Error()
		return
	}

	log.Printf("Call SetVip return, %v", r)

	if m, err := proto.Marshal(r); err != nil {
		msg = "Marshal UserStatList Error"
	} else {
		ret, msg = "OK", base64.StdEncoding.EncodeToString(m)
	}
	return
}

func Call_ReportVideoCallStatus(c pb.HospitalClient, ctx context.Context, param []byte) (ret, msg string) {
	ret, msg = "ERROR", "unknown"
	d := &pb.ShiPinYongHuL5{}
	if err := proto.Unmarshal(param, d); err != nil {
		msg = "Unmarshal ShiPinYongHuL5 Error"
		return
	}
	log.Printf("Call ReportVideoCallStatus, %v", d)
	r, err := c.ReportVideoCallStatus(ctx, d)
	if err != nil {
		msg = "Call ReportVideoCallStatus error:" + err.Error()
		return
	}

	log.Printf("Call ReportVideoCallStatus return, %v", r)

	if m, err := proto.Marshal(r); err != nil {
		msg = "Marshal ReturnMessage Error"
	} else {
		ret, msg = "OK", base64.StdEncoding.EncodeToString(m)
	}
	return
}

func Call_GetTitle(c pb.HospitalClient, ctx context.Context, param []byte) (ret, msg string) {
	ret, msg = "ERROR", "unknown"
	d := &pb.MenuTitle{}
	if err := proto.Unmarshal(param, d); err != nil {
		msg = "Unmarshal MenuTitle Error"
		return
	}
	log.Printf("Call GetTitle, %v", d)
	r, err := c.GetTitle(ctx, d)
	if err != nil {
		msg = "Call GetTitle error:" + err.Error()
		return
	}

	log.Printf("Call GetTitle return, %v", r)

	if m, err := proto.Marshal(r); err != nil {
		msg = "Marshal UserStatList Error"
	} else {
		ret, msg = "OK", base64.StdEncoding.EncodeToString(m)
	}
	return
}

func Call_GetTitleV2(c pb.HospitalClient, ctx context.Context, param []byte) (ret, msg string) {
	ret, msg = "ERROR", "unknown"
	d := &pb.MenuTitle{}
	if err := proto.Unmarshal(param, d); err != nil {
		msg = "Unmarshal MenuTitle Error"
		return
	}
	log.Printf("Call GetTitleV2, %v", d)
	r, err := c.GetTitleV2(ctx, d)
	if err != nil {
		msg = "Call GetTitleV2 error:" + err.Error()
		return
	}

	log.Printf("Call GetTitleV2 return, %v", r)

	if m, err := proto.Marshal(r); err != nil {
		msg = "Marshal UserStatList Error"
	} else {
		ret, msg = "OK", base64.StdEncoding.EncodeToString(m)
	}
	return
}

func CallGRPC(method string, param []byte) (string, string) {
	// Set up a connection to the server.
	conn, err := grpc.Dial(address, grpc.WithInsecure())
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	c := pb.NewHospitalClient(conn)
	// Contact the server and print out its response.
	ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
	defer cancel()
	switch string(method) {
	case "Login":
		return Call_Login(c, ctx, param)
	case "HuanZheRegister":
		return Call_HuanZheRegister(c, ctx, param)
	case "HuanZheHelpRegister":
		return Call_HuanZheHelpRegister(c, ctx, param)
	case "GetHuanZheInfo":
		return Call_GetHuanZheInfo(c, ctx, param)
	case "SaveHuanZheInfo":
		return Call_SaveHuanZheInfo(c, ctx, param)
	case "GetWXSession":
		return Call_GetWXSession(c, ctx, param)
	case "DecodeWXUserInfo":
		return Call_DecodeWXUserInfo(c, ctx, param)
	case "CallZYAPI":
		return Call_CallZYAPI(c, ctx, param)
	case "MakeVideoCall":
		return Call_MakeVideoCall(c, ctx, param)
	case "AcceptVideoCall":
		return Call_AcceptVideoCall(c, ctx, param)
	case "HangUpVideoCall":
		return Call_HangUpVideoCall(c, ctx, param)
	case "RequestJoinVideoCall":
		return Call_RequestJoinVideoCall(c, ctx, param)
	case "CancelVideoCallRequest":
		return Call_CancelVideoCallRequest(c, ctx, param)
	case "RequestHangUpVideoCall":
		return Call_RequestHangUpVideoCall(c, ctx, param)
	case "GetVideoCallRequestList":
		return Call_GetVideoCallRequestList(c, ctx, param)
	case "TakeVideoCallRequest":
		return Call_TakeVideoCallRequest(c, ctx, param)
	case "SaveShiPinRequestL6":
		return Call_SaveShiPinRequestL6(c, ctx, param)
	case "RemoveVideoCallRequest":
		return Call_RemoveVideoCallRequest(c, ctx, param)
	case "GenerateUserSig":
		return Call_GenerateUserSig(c, ctx, param)
	case "GetYiShengInfo":
		return Call_GetYiShengInfo(c, ctx, param)
	case "GetKeFuInfo":
		return Call_GetKeFuInfo(c, ctx, param)
	case "GetGuaHaoList":
		return Call_GetGuaHaoList(c, ctx, param)
	case "GetGuaHaoDuiZhangList":
		return Call_GetGuaHaoDuiZhangList(c, ctx, param)
	case "GetShiPinList":
		return Call_GetShiPinList(c, ctx, param)
	case "GetMenZhenPaiBanList":
		return Call_GetMenZhenPaiBanList(c, ctx, param)
	case "SaveGuaHao":
		return Call_SaveGuaHao(c, ctx, param)
	case "MenZhenCurrent":
		return Call_MenZhenCurrent(c, ctx, param)
	case "MenZhenNext":
		return Call_MenZhenNext(c, ctx, param)
	case "MenZhenJiaoHao":
		return Call_MenZhenJiaoHao(c, ctx, param)
	case "MenZhenGuoHao":
		return Call_MenZhenGuoHao(c, ctx, param)
	case "MenZhenQuery":
		return Call_MenZhenQuery(c, ctx, param)
	case "MenZhenWanCheng":
		return Call_MenZhenWanCheng(c, ctx, param)
	case "MenZhenInform":
		return Call_MenZhenInform(c, ctx, param)
	case "DengLuShuJuVerify":
		return Call_DengLuShuJuVerify(c, ctx, param)
	case "GetHouZhenPaiDuiList":
		return Call_GetHouZhenPaiDuiList(c, ctx, param)
	case "GetVideoCallNeedResume":
		return Call_GetVideoCallNeedResume(c, ctx, param)
	case "PaiDuiJiaoHao":
		return Call_PaiDuiJiaoHao(c, ctx, param)
	case "PaiDuiSetStatus":
		return Call_PaiDuiSetStatus(c, ctx, param)
	case "SaveGuaHaoShuJu":
		return Call_SaveGuaHaoShuJu(c, ctx, param)
	case "GetGuaHaoShuJu":
		return Call_GetGuaHaoShuJu(c, ctx, param)
	case "GetGetHospital":
		return Call_GetHospital(c, ctx, param)
	case "GetHospitalWithinDistance":
		return Call_GetHospitalWithinDistance(c, ctx, param)
	case "GetHospitalByArea":
		return Call_GetHospitalByArea(c, ctx, param)
	case "GetUserStat":
		return Call_GetUserStat(c, ctx, param)
	case "ReportVideoCallStatus":
		return Call_ReportVideoCallStatus(c, ctx, param)
	case "GetVip":
		return Call_GetVip(c, ctx, param)
	case "SetVip":
		return Call_SetVip(c, ctx, param)
	case "GetTitle":
		return Call_GetTitle(c, ctx, param)
	case "GetTitleV2":
		return Call_GetTitleV2(c, ctx, param)

	default:
		log.Println("Unknown Method", string(method))
	}

	return "ERROR", "UNKNOWN"
}

func RestAPI(method, param_base64 string) (string, string) {
	log.Println("method:", method)
	param, err := base64.StdEncoding.DecodeString(param_base64)
	if err != nil {
		log.Println("Cannot decode param", err)
		return "PARAM DECODE ERROR", ""
	}
	// log.Println("method:", method, param)
	return CallGRPC(method, param)
}

// test command:
// curl localhost:10008/grpcservice -F method=SaveShiPinInfo -F param=CAESBjEyMzQ1NhoFS2VGdTEiBgjmn9rtBTAB
// curl localhost:10008/grpcservice -F method=SaveShiPinInfo -F param=CAISBjU0NDMzMxoFS2VGdTIiBgjxs9rtBTABQAE=
// curl localhost:10008/grpcservice -F method=GetShiPinByID -F param=CAE=
// curl localhost:10008/grpcservice -F method=UpdateShiPinInfo -F param=CAISBjU0NDMzMxoFS2VGdTIiBgiiv9rtBSoGCKK_2u0FQAE=
// curl localhost:10008/grpcservice -F method=GetShiPinByID -F param=CAI=
func gRPCService(c *gin.Context) {
	denglu := c.PostForm("denglu")
	method := c.PostForm("method")
	param := c.PostForm("param")
	log.Println("DengLu:", denglu)
	NotVerifyMethods := [...]string{"Login", "HuanZheRegister", "GetWXSession", "DecodeWXUserInfo"}
	skipVerify := false
	for _, m := range NotVerifyMethods {
		if method == m {
			skipVerify = true
			break
		}
	}
	if skipVerify == false {
		if r, _ := RestAPI("DengLuShuJuVerify", denglu); r == "ERROR" {
			log.Println("Verify DengLuShuJu FAIL，Method:", method)
			c.JSON(http.StatusOK, gin.H{"RESULT": "ERROR", "MSG": "TOKEN ERROR"})
			return
		} else {
			log.Println("Verify DengLu OK")
		}
	}
	result, msg := RestAPI(method, param)
	c.JSON(http.StatusOK, gin.H{"RESULT": result, "MSG": msg})
}

// type JsonRequest struct {
// 	Denglu string `json:denglu`
// 	Method string `json:method`
// 	Param  string `json:param`
// }

// func gRPCJSonService(c *gin.Context) {
// 	log.Println("gRPCJSonService:", c)
// 	decoder := json.NewDecoder(c.Request.Body)
// 	var m JsonRequest
// 	err := decoder.Decode(&m)
// 	if err != nil {
// 		log.Println("err:", err)
// 		c.JSON(http.StatusOK, gin.H{"RESULT": "ERROR", "MSG": "DECODE ERROR"})
// 	} else {
// 		// log.Println("m:", m)
// 		result, msg := RestAPI(m.Method, m.Param)
// 		log.Println("ret:", result, msg)
// 		c.JSON(http.StatusOK, gin.H{"RESULT": result, "MSG": msg})
// 	}
// }

var UploadFilePath = filepath.Join("..", "bingli")

func upload(c *gin.Context) {
	log.Println("upload")
	file, header, err := c.Request.FormFile("file")
	//log.Println(file, header, err)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "Form Error:" + err.Error()})
		log.Println(err)
		return
	}
	filename := header.Filename
	destname := filepath.Join(UploadFilePath, filename)
	// filename := fmt.Sprint(time.Now().UnixNano()) + filepath.Ext(header.Filename)
	log.Println("dest:" + destname)
	out, err := os.Create(destname)
	if err != nil {
		log.Println(err)
		c.JSON(http.StatusInternalServerError, gin.H{"error": "Create Error:" + err.Error()})
		return
	}
	defer out.Close()
	_, err = io.Copy(out, file)
	if err != nil {
		log.Println(err)
		c.JSON(http.StatusInternalServerError, gin.H{"error": "Copy Error:" + err.Error()})
		return
	}
	c.JSON(http.StatusOK, gin.H{"filepath": filename})
}

func main() {
	log.Println("gRPC Rest Service Run ...")
	// b := "EgYxMjM0NTYaB3lvbmdodTEiBgi5wN/tBQ=="
	// if param, err := base64.StdEncoding.DecodeString(b); err != nil {
	// 	log.Println("ERROR:", err)
	// } else {
	// 	log.Println("PARAM:", param)
	// }

	r := gin.Default()
	r.Use(cors.New(cors.Config{
		AllowAllOrigins: true,
	}))

	r.POST("/grpcservice", gRPCService)
	//C# FormUrlEncodedContent will return URI too long error, use JSON instead
	// r.POST("/grpcjson", gRPCJSonService)
	r.StaticFS("/site", http.Dir("./site"))
	r.StaticFS("/bingli", http.Dir("../bingli"))
	r.POST("/upload", upload)

	// r.Run(":10008")

	r.Use(TlsHandler())
	r.RunTLS(":443", "1_hosapi.tianfulei.cn_bundle.crt", "2_hosapi.tianfulei.cn.key")
}

func TlsHandler() gin.HandlerFunc {
	return func(c *gin.Context) {
		secureMiddleware := secure.New(secure.Options{
			SSLRedirect: true,
			SSLHost:     "localhost:443",
		})
		err := secureMiddleware.Process(c.Writer, c.Request)

		// If there was an error, do not continue.
		if err != nil {
			return
		}

		c.Next()
	}
}
